package com.yyydjk.library;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by xujl on 2018/2/7.
 * tab菜单代理
 */

public class MenuTabDelegate implements TabSwitchListener {
    /**
     * 顶部菜单布局
     */
    private LinearLayout tabMenuView;
    /**
     * 菜单数据
     */
    private List tabData;
    private List<Class> tabClassList;
    /**
     * 分割线颜色
     */
    private int dividerColor = 0xffcccccc;
    private List<TabClickListener> mTabClickListeners;
    private List<ITabView> mTabViewList;
    /**
     * 被点击的tab下标
     */
    private int clickIndex = -1;

    public View initMenu (Context context, int menuBackgroundColor) {
        //初始化tabMenuView并添加到tabMenuView
        tabMenuView = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tabMenuView.setOrientation(LinearLayout.HORIZONTAL);
        tabMenuView.setGravity(Gravity.CENTER_VERTICAL);
        tabMenuView.setBackgroundColor(menuBackgroundColor);
        tabMenuView.setLayoutParams(params);
        mTabViewList = new ArrayList<>();
        mTabClickListeners = new ArrayList<>();
        return tabMenuView;
    }


    public void setTabData (List tabData) {
        this.tabData = tabData;
    }

    public void addTabClickListener (TabClickListener tabClickListener) {
        mTabClickListeners.add(tabClickListener);
    }

    public void setTabClassList (Class... cls) {
        /*
         *  如果没有指定tab控件类型，或者指定的类型与实体数量不匹配
         *  则自动生成默认tab类型
         */
        if (cls == null || cls.length == 0 || cls.length != tabData.size()) {
            this.tabClassList = new ArrayList<>();
            for (Object o : tabData) {
                tabClassList.add(DefaultTabView.class);
            }
            return;
        }
        tabClassList = Arrays.asList(cls);
    }

    /**
     * 添加标签
     */
    public void addTabViews (Context context) {
        for (int i = 0; i < tabClassList.size(); i++) {
            final Class aClass = tabClassList.get(i);
            addTabView(context, i, aClass);
        }

    }

    private void addTabView (Context context, final int index, Class cls) {
        try {

            final Constructor constructor = cls.getConstructor(Context.class);
            final ITabView tab = (ITabView) constructor.newInstance(context);
            tab.setTabContent(tabData.get(index));
            //添加点击事件
            tab.setClickListener(new View.OnClickListener() {
                @Override
                public void onClick (View v) {
                    onTabSwitch(index,tab);
                    for (TabClickListener tabClickListener : mTabClickListeners) {
                        tabClickListener.onTabClick(index, tab);
                    }
                }
            });
            tabMenuView.addView((View) tab);
            mTabViewList.add(tab);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            throw new ClassCastException("tab菜单View必须实现ITabView接口");
        }

        //添加分割线
        if (index < tabData.size() - 1) {
            View view = new View(context);
            view.setLayoutParams(new LinearLayout.LayoutParams(DeviceUtils.dpTpPx(0.5f, context), ViewGroup.LayoutParams.MATCH_PARENT));
            view.setBackgroundColor(dividerColor);
            tabMenuView.addView(view);
        }
    }

    public int getDividerColor () {
        return dividerColor;
    }

    public void setDividerColor (int dividerColor) {
        this.dividerColor = dividerColor;
    }

    public <T> void setTabText (int index, T data) {
        if (index == -1) {
            if (clickIndex == -1) {
                return;
            }
            index = clickIndex;
        }

        mTabViewList.get(index).setTabContent(data);
    }

    public int getClickIndex () {
        return clickIndex;
    }

    public void closeMenu () {
        mTabViewList.get(clickIndex).unChecked();
        clickIndex = -1;
    }

    public void setClickIndex (int clickIndex) {
        this.clickIndex = clickIndex;
    }

    @Override
    public void onTabSwitch (int newPosition, ITabView newView) {
        for (int i = 0; i < mTabViewList.size(); i++) {
            final ITabView iTabView = mTabViewList.get(i);
            if (i == newPosition) {
                iTabView.checked();
            } else {
                iTabView.unChecked();
            }
        }
    }
}
